﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using BO = Viewer.BusinessObject.BusinessEntities;
namespace Viewer.ToChuc.UserControls
{
    public partial class UCDiaChi : System.Web.UI.UserControl
    {
        BO.CommonCode common = new Viewer.BusinessObject.BusinessEntities.CommonCode();
        BO.DiaChi diachi = new Viewer.BusinessObject.BusinessEntities.DiaChi();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadDataControl();
            }
        }
        void LoadDataControl()
        {
            storeTinh.DataSource = common.LoadToChucHanhChinhByLoai(1242);
            storeTinh.DataBind();
        }

        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            int xa = !string.IsNullOrEmpty(cbbXa.SelectedItem.Value) ? Convert.ToInt16(cbbXa.SelectedItem.Value) : 0;

            int quanHuyen = !string.IsNullOrEmpty(cbbQuan.SelectedItem.Value) ? Convert.ToInt16(cbbQuan.SelectedItem.Value) : 0;
            int thanhPho = !string.IsNullOrEmpty(cbbTinh.SelectedItem.Value) ? Convert.ToInt16(cbbTinh.SelectedItem.Value) : 0;
            if (hdStatus.Text.Equals("AddNew"))
            {
                diachi.InsertDiaChi(txtSoNha.Text, xa, txtThonPho.Text, quanHuyen, thanhPho, txtGhiChu.Text);
            }
            else
            {
                diachi.UpdateDiachi(Convert.ToInt16(txtId.Text), txtSoNha.Text, xa, txtThonPho.Text, quanHuyen, thanhPho, txtGhiChu.Text);
            }
            ((UCLookUpDiaChi)this.Parent).ShowDialogDiaChi();
            X.Mask.Hide();
            wdDiaChi.Hide();
        }
        private void ResetControl()
        {
            txtGhiChu.Text = string.Empty;
            txtSoNha.Text = string.Empty;
            txtThonPho.Text = string.Empty;
            cbbXa.SelectedItem.Value = string.Empty;
            cbbQuan.SelectedItem.Value = string.Empty;
            cbbTinh.SelectedItem.Value = string.Empty;
        }
        [DirectMethod]
        public void Show(string type, int diachiId)
        {
            if (type.Equals("AddNew"))
            {
                ResetControl();
                hdStatus.Text = "AddNew";
            }
            else if (type.Equals("Edit"))
            {
                BO.CHDiaChi item = diachi.LoadDataByDiaChiId(diachiId);
                txtGhiChu.Text = item.GhiChu;
                txtSoNha.Text = item.SoNha;

                cbbTinh.SelectedItem.Value = item.TinhTPID > 0 ? item.TinhTPID.ToString() : string.Empty;

                LoadQuan(item.TinhTPID, "Quan");
                cbbQuan.SelectedItem.Value = item.QuanHuyenID > 0 ? item.QuanHuyenID.ToString() : string.Empty;

                LoadQuan(item.QuanHuyenID, "Xa");
                cbbXa.SelectedItem.Value = item.XaPhuongID > 0 ? item.XaPhuongID.ToString() : string.Empty;

                LoadQuan(item.XaPhuongID, "Thon");
                txtThonPho.Text = item.ThonPho;

                txtId.Text = item.DiaChiID.ToString();
                hdStatus.Text = "Edit";
            }

            wdDiaChi.Show();
        }
        [DirectMethod]
        public void LoadQuan(int key, string type)
        {
            if (type.Equals("Quan"))
            {
                storeQuan.DataSource = common.LoadToChucHanhChinhByToChucCha(key);
                storeQuan.DataBind();
                cbbQuan.Text = string.Empty;
                cbbXa.Text = string.Empty;
            }
            else if (type.Equals("Xa"))
            {
                storeXa.DataSource = common.LoadToChucHanhChinhByToChucCha(key);
                storeXa.DataBind();
                cbbXa.Text = string.Empty;
            }

        }
    }
}